############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A2: Threshold Analysis
############################################################

# Required Libraries
require(ggplot2)

# Load Functions #
source('FragaJuenkeShah_Functions.R')

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

# Change candidate variable to a binary, 0 = no candidate, 1 = one or more candidates
sl_data$white_cand[sl_data$white_cand > 1] <- 1
sl_data$black_cand[sl_data$black_cand > 1] <- 1
sl_data$latino_cand[sl_data$latino_cand > 1] <- 1
sl_data$asian_cand[sl_data$asian_cand > 1] <- 1

# Get point of equality at varying thresholds (WARNING: takes 1hr to run with 4.2GHz Core i7)
threshW <- getThresholdsW(0.01, 0.95, sl_data)
threshB <- getThresholdsB(0.01, 0.95, sl_data)
threshL <- getThresholdsL(0.01, 0.95, sl_data)
threshA <- getThresholdsA(0.01, 0.95, sl_data)

getDiff <- function(thresh){
	thresh$Diff <- thresh[,3] - thresh[,1]
	thresh$Diff_SE <- sqrt((thresh[,4]^2) + (thresh[,2]^2))
	thresh$Diff_Lo <- thresh$Diff - qnorm(0.975)*thresh$Diff_SE
	thresh$Diff_Hi <- thresh$Diff + qnorm(0.975)*thresh$Diff_SE
	return(thresh)
}

threshW <- getDiff(threshW)
threshB <- getDiff(threshB)
threshL <- getDiff(threshL)
threshA <- getDiff(threshA)

## Fig A4a, Upper Left Panel: Whites
figA4a <- ggplot(data=threshW, aes(x=Threshold, y=Diff*100))
figA4a <- figA4a + geom_line(size=0.5, alpha=1, color="black")
figA4a <- figA4a + geom_hline(yintercept=c(0), linetype=3, color="red")
figA4a <- figA4a + geom_ribbon(aes(x=Threshold, ymax=Diff_Hi*100, ymin=Diff_Lo*100), alpha=0.15, fill="gray50")
figA4a <- figA4a + ggtitle("Whites")
figA4a <- figA4a + scale_x_continuous("Threshold for Congressional District Overlap")
figA4a <- figA4a + scale_y_continuous("Co-Ethnic Incumbent Difference", limits=c(-20,5))
figA4a <- figA4a + geom_segment(aes(x=0.25, xend=0.25, y=subset(threshW, Threshold==0.25)$Diff_Lo*100, yend=subset(threshW, Threshold==0.25)$Diff_Hi*100), color="blue")
figA4a <- figA4a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA4a

## Fig A4b, Upper Right Panel: African-Americans
figA4b <- ggplot(data=threshB, aes(x=Threshold, y=Diff*100))
figA4b <- figA4b + geom_line(size=0.5, alpha=1, color="black")
figA4b <- figA4b + geom_hline(yintercept=c(0), linetype=3, color="red")
figA4b <- figA4b + geom_ribbon(aes(x=Threshold, ymax=Diff_Hi*100, ymin=Diff_Lo*100), alpha=0.15, fill="gray50")
figA4b <- figA4b + ggtitle("African-Americans")
figA4b <- figA4b + scale_x_continuous("Threshold for Congressional District Overlap")
figA4b <- figA4b + scale_y_continuous("Co-Ethnic Incumbent Difference", limits=c(-20,5))
figA4b <- figA4b + geom_segment(aes(x=0.25, xend=0.25, y=subset(threshB, Threshold==0.25)$Diff_Lo*100, yend=subset(threshB, Threshold==0.25)$Diff_Hi*100), color="blue")
figA4b <- figA4b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA4b

## Fig A4c, Lower Left Panel: Latinos
figA4c <- ggplot(data=threshL, aes(x=Threshold, y=Diff*100))
figA4c <- figA4c + geom_line(size=0.5, alpha=1, color="black")
figA4c <- figA4c + geom_hline(yintercept=c(0), linetype=3, color="red")
figA4c <- figA4c + geom_ribbon(aes(x=Threshold, ymax=Diff_Hi*100, ymin=Diff_Lo*100), alpha=0.15, fill="gray50")
figA4c <- figA4c + ggtitle("Latinos")
figA4c <- figA4c + scale_x_continuous("Threshold for Congressional District Overlap")
figA4c <- figA4c + scale_y_continuous("Co-Ethnic Incumbent Difference", limits=c(-20,5))
figA4c <- figA4c + geom_segment(aes(x=0.25, xend=0.25, y=subset(threshL, Threshold==0.25)$Diff_Lo*100, yend=subset(threshL, Threshold==0.25)$Diff_Hi*100), color="blue")
figA4c <- figA4c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA4c

## Fig A4d, Lower Right Panel: Asians
threshA$Diff_Lo[threshA$Diff_Lo < -0.30] <- -0.30
threshA$Diff_Hi[threshA$Diff_Hi > 0.30] <- 0.30

figA4d <- ggplot(data=threshA, aes(x=Threshold, y=Diff*100))
figA4d <- figA4d + geom_line(size=0.5, alpha=1, color="black")
figA4d <- figA4d + geom_hline(yintercept=c(0), linetype=3, color="red")
figA4d <- figA4d + geom_ribbon(aes(x=Threshold, ymax=Diff_Hi*100, ymin=Diff_Lo*100), alpha=0.15, fill="gray50")
figA4d <- figA4d + ggtitle("Asians")
figA4d <- figA4d + scale_x_continuous("Threshold for Congressional District Overlap")
figA4d <- figA4d + scale_y_continuous("Co-Ethnic Incumbent Difference", limits=c(-31,30), breaks=seq(-30,30,10))
figA4d <- figA4d + geom_segment(aes(x=0.25, xend=0.25, y=subset(threshA, Threshold==0.25)$Diff_Lo*100, yend=subset(threshA, Threshold==0.25)$Diff_Hi*100), color="blue")
figA4d <- figA4d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA4d